const cartModel = require("../models/cart_model")
const Result = require("../beans/result")
const mongoUtil = require("../models/mongo_util")
// 添加商品到购物车
function AddCartlist(useridgoods, callback) {

    cartModel.getCart(useridgoods, (err, data) => {
        console.log("dataddatdat", data);
        if (data.length) {
            callback(new Result(null, data, "插入购物车数据成功", 200))
        } else {
            callback(new Result(err, {}, "添加购物车数据成功", 200))
        }
    })
}
// 获取购物车商品
function getCartlist(cart, callback) {
    cartModel.getCart(cart, (err, data) => {
        if (data.length && data[0].userid == cart.userid) {
            callback(new Result(null, data, "购物车信息获取成功", 200))
        } else {
            callback(new Result(err, null, "购物车信息获取失败", 401))
        }

    })
}
// 修改商品数量
function updatacartgoodsqty(cart, callback) {
    cartModel.getCart(cart, (err, data) => {
        if (data.length && data[0].userid == cart.userid) {
            callback(new Result(null, data, "修改购物车商品数量成功", 200))
        } else {
            // console.log("kkkkkkk",data);
            callback(new Result(err, null, "修改购物车商品数量失败", 401))
        }
    })
}
// 删除购物车商品
function deletecartgoods(delgoods,callback){
    cartModel.getCart(delgoods,(err,data)=>{
        data.forEach(item=>{
            if (data.length && item.userid == delgoods.userid) {
            callback(new Result(null, data, "删除购物车商品成功", 200))
        } else {
            // console.log("kkkkkkk",data);
            callback(new Result(err, null, "删除购物车商品失败", 401))
        }
        })
        
    })
}

module.exports = {
    AddCartlist,
    getCartlist,
    updatacartgoodsqty,
    deletecartgoods
}